/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(function(){
    var user_id = parseInt(getURLParameter("user"));
    var track_name = getURLParameter("track");
    
    getButtons(user_id);

    $("#track_title").text(track_name);

    // Get deferred object for a run based on track, when it's done do...
    getRunsByTrack(user_id, track_name).done(function(runs) {
        
        // List runs 
        var runs_html = "<ul>";
        for (var j = 0; j < runs.length; j++) {
            var date = new Date(runs[j].runDate);
            runs_html += "<li><a href='run.jsp?user=" + user_id + "&run=" + runs[j].id + "'>";
            runs_html += date.toUTCString() + "</a></li>";
        }
        runs_html += "</ul>";
        $("#run_list").append(runs_html);

        var datetime = [];

        // Get date/times
        for (i = 0; i < runs.length; i++) {
            datetime.push([runs[i].runDate, runs[i].totalTime]);
        }

        // Set options for plot
        plot_options.title = "Runtime of track";
        plot_options.axes = {
            xaxis: {
                renderer: $.jqplot.DateAxisRenderer
            }
        }
        plot_options.seriesDefaults.markerOptions.show = true;
        $.jqplot('time_plot', [datetime], plot_options);
    });
});


